import Vue from 'vue'
import Vuex from 'vuex';
import { asyncRoutes, constantRouterMap } from "@/router/index.js"
import { getCookie } from '../util/common';
Vue.use(Vuex);

function hasPermission(roles, route) {
    if (route.meta && route.meta.roles) {
        return roles.some(role => route.meta.roles.includes(role))
    } else {
        return true
    }
}

export function filterAsyncRoutes(routes, roles) {
    const res = []

    routes.forEach(route => {
        const tmp = { ...route }
        if (hasPermission(roles, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles)
            }
            res.push(tmp)
        }
    })

    return res
}

function hasPermissionByOffice(office, route) {
    if (route.meta && route.meta.office) {
        console.log(route.meta.office.includes(office));
        return route.meta.office.includes(office);
    } else {
        return true
    }
}
function routesChange(ruote1, route2) {
    console.log(ruote1)
    for (let i = 0; i < ruote1.length; i++) {
        for (let j = 0; j < route2.length; i++) {
            if (ruote1[i].name = route2[j].name) {
                ruote1[i].children.concat(route2[j].children)
            }
        }
    }
    return ruote1;
}
export function filterAsyncRoutesByOffice(routes, office) {
    const res = []

    routes.forEach(route => {
        const tmp = { ...route }
        if (hasPermissionByOffice(office, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutesByOffice(tmp.children, office)
            }
            res.push(tmp)
        }
    })

    return res;
}

const getDefaultState = () => {
    return {
        loginUser: {
            data: null,
        },
        roles: [],
        routes: [],
        // 角色数据库
        rolesDataList: [],
        addRoutes: [],
        offices: [],
    }
}
const store = new Vuex.Store({
    state: {
        loginUser: {
            data: null,
        },
        roles: [],
        routes: [],
        // 角色数据库
        rolesDataList: [],
        addRoutes: [],
        offices: [],
        winWidth: 0,
        winHeight: 0,
    },
    getters: {
        roles: state => {
            return state.roles;
        },
        loginUser: state => {
            return state.loginUser;
        },
        routes: state => {
            return state.routes;
        },
        offices: state => {
            return state.offices;
        },
        rolesDataList: state => {
            return state.rolesDataList;
        },
        winWidth: state => {
            return state.winWidth;
        },
        winHeight: state => {
            return state.winHeight;
        }

    },
    mutations: {
        reset_state: (state) => {
            Object.assign(state, getDefaultState())
        },
        setLoginUser(state, payload) {
            if (payload) {
                state.loginUser.data = payload;
            } else {
                let temp = getCookie("currentUser");
                let loginUser = JSON.parse(temp);
                state.loginUser.data = loginUser;
            }
        },
        setOffices(state, payload) {
            if (payload) {
                state.offices = payload;
            } else {
                let temp = getCookie("offices");
                let offices = JSON.parse(temp);
                state.offices = offices;
            }
        },
        // 设置当前角色的用户表
        setRoles(state, info) {
            if (info) { state.roles = [] } else {
                for (let i = 0; i < state.loginUser.data.roles.length; i++) {
                    state.roles.push(state.loginUser.data.roles[i].name);
                }
            }

        },
        setRolesDataList(state, info) {
            for (let i = 0; i < info.length; i++) {
                state.rolesDataList.push(info[i])
            }
        },
        SET_ROUTES: (state, routes) => {
            state.addRoutes = routes
            // 生成用于导航栏的数据
            state.routes = routes;
            // state.routes = constantRouterMap.concat(routes)
        },
        setWinWidth: (state, info) => {
            state.winWidth = info;
        },
        setWinHeight: (state, info) => {
            state.winHeight = info;
        }
    },
    actions: {
        generateRoutes({ commit }, userInfo) {
            let roles = userInfo.roles;
            let office = userInfo.office;
            return new Promise(resolve => {
                let accessedRoutes
                console.log("vuex", office)
                if (roles.includes("超级管理员")) {
                    accessedRoutes = asyncRoutes || []
                } else {
                    accessedRoutes = filterAsyncRoutesByOffice(filterAsyncRoutes(asyncRoutes, roles), office)

                }
                commit('SET_ROUTES', accessedRoutes)
                resolve(accessedRoutes)
            })
        },
        logout({ commit }) {
            return new Promise((resolve, reject) => {
                try {
                    commit("reset_state")
                    resolve()
                } catch (error) {
                    reject(error)
                }
            })
        },

    }
})

export default store;